package com.dldata.display.control.System;

import com.dldata.display.entity.SysStaffEntity;
import com.dldata.display.entity.SysUserOnlineEntity;
import com.dldata.display.service.System.SysStaffService;
import com.dldata.display.service.System.SysUserOnlineService;
import com.dldata.display.utils.StringUtil;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by 倪继文 on 2016/7/21.
 */

@Controller
@RequestMapping(value = "/SysUserOnline")
public class SysUserOnlineController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SysUserOnlineService sysUserOnlineService;

    @Autowired
    SysStaffService staffService;

//    @RequestMapping(value = "/gotoUserOnline")
//    public String gotoUserOnline(Model model){
//        model.addAttribute("titlename","在线用户列表");
//        return "System/SysUserOnline";
//    }


    @RequestMapping(value = "/getAllUserOnline")
    @ResponseBody
    public String  getAllUserOnline(HttpServletRequest request, HttpServletResponse response)throws Exception{

        List<SysUserOnlineEntity> sysUserOnline = sysUserOnlineService.getAllUserOnline();
        List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
        List<SysStaffEntity> staffEntities = staffService.getAllStaff();
        String result;
        try{
            if (sysUserOnline.size() > 0) {
                sysUserOnline.forEach((u)-> {
                    Map<String,String> map = new HashMap<String, String>();
                    map.put("userId",u.getUserId());
                    map.put("loginTime",u.getLoginTime().toString());
                    map.put("sysCode", u.getSysCode());
                    map.put("userName","");
                    staffEntities.stream().filter(s -> s.getId().equals(u.getUserId())).limit(1).forEach(s -> {
                        map.put("userName", s.getName());
                    });
                    mapList.add(map);
                });
            }
        }
        catch(Exception ex){
            result = "{\"success\":false,\"msg\":\"系统错误，请稍候再试！\""+ ex.getMessage().replace("\r","").replace("\n","")+"}";
            StringUtil.writeToWeb(result, "html", response);
            logger.error("【执行"+this.getClass().getName()+".getAllUserOnline() 方法失败 】异常："+ex.getMessage());
        }
        return  mapList.size()>0 ? JSONArray.fromObject(mapList).toString() : "[]";
    }

    @RequestMapping(value = "/findByCondition")
    @ResponseBody
    public String findByCondition(String condition,HttpServletRequest request,HttpServletResponse response) throws Exception{
        List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
        List<SysStaffEntity> staffEntities = staffService.getAllStaff();
        List<SysUserOnlineEntity> sysUserOnline = sysUserOnlineService.findByCondition("%" + condition + "%");
        String result;
        try{
            if (sysUserOnline.size() > 0) {
                sysUserOnline.forEach((u)-> {
                    Map<String,String> map = new HashMap<String, String>();
                    map.put("userId", u.getUserId());
                    map.put("loginTime", u.getLoginTime().toString());
                    map.put("sysCode", u.getSysCode());
                    map.put("userName","");
                    staffEntities.stream().filter(s -> s.getId().equals(u.getUserId())).limit(1).forEach(s -> {
                        map.put("userName", s.getName());
                    });
                    mapList.add(map);
                });
            }
        }
        catch(Exception ex){
            result = "{\"success\":false,\"msg\":\"系统错误，请稍候再试！\""+ ex.getMessage().replace("\r","").replace("\n","")+"}";
            StringUtil.writeToWeb(result, "html", response);
            logger.error("【执行"+this.getClass().getName()+".findRoleByCondition() 方法失败 】异常："+ex.getMessage());
        }
        return  mapList.size()>0 ? JSONArray.fromObject(mapList).toString() : "[]";
    }

    // 对应页面展示列表
//    private class ShowUserOnline{
//        private String userId;
//        private String userName;
//        private Timestamp loginTime;
//        private String sysCode;
//
//        public String getUserId() {
//            return userId;
//        }
//
//        public void setUserId(String userId) {
//            this.userId = userId;
//        }
//
//        public String getUserName() {
//            return userName;
//        }
//
//        public void setUserName(String userName) {
//            this.userName = userName;
//        }
//
//        public Timestamp getLoginTime() {
//            return loginTime;
//        }
//
//        public void setLoginTime(Timestamp loginTime) {
//            this.loginTime = loginTime;
//        }
//
//        public String getSysCode() {
//            return sysCode;
//        }
//
//        public void setSysCode(String sysCode) {
//            this.sysCode = sysCode;
//        }
//    }
}
